﻿using MongoDB.Driver;

namespace CQRS.Events
{
    public class MongoDbEventDatabaseCreator : IEventDatabaseCreator
    {
        private readonly IMongoDatabaseHelper _mongoDatabaseHelper;

        public MongoDbEventDatabaseCreator(IMongoDatabaseHelper mongoDatabaseHelper)
        {
            _mongoDatabaseHelper = mongoDatabaseHelper;
        }

        #region IEventDatabaseCreator Members

        public void Create()
        {
            CreateDatabaseSchema();
        }

        #endregion

        protected void CreateDatabaseSchema()
        {
            MongoDatabase mongoDatabase = _mongoDatabaseHelper.GetMongoDatabase();
            if (!mongoDatabase.CollectionExists("Events"))
                mongoDatabase.CreateCollection("Events");
            if (!mongoDatabase.CollectionExists("EventProviders"))
                mongoDatabase.CreateCollection("EventProviders");
            if (!mongoDatabase.CollectionExists("Snapshots"))
                mongoDatabase.CreateCollection("Snapshots");
        }
    }
}